package com.lyy.controller;

import cn.hutool.core.bean.BeanUtil;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.lyy.entity.param.UserLoginParam;
import com.lyy.entity.vo.Uservo;
import com.lyy.result.Result;
import lombok.Data;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Liangyaoyun
 */
@Controller

@RequestMapping("/api/v1/temp")
public class TempController {
    //  @RequestMapping("/forward")
//  public String froward(){
//          return "forward:/view/main.html";
//          //请求转发效率高，只发送一次请求
//      }
//  @RequestMapping("/redirect")
//  public String redirect(){
//      return "redirect:/view/main.html";
//      //重定向效率低，地址栏发生变化，添加业务防刷新
//  }
//  //@RequestBody返回数据，不加调到页面
//  @ResponseBody
//  @RequestMapping("/list")
//  public User list(){
//      User user = new User();
//      user.setUsername("小丑");
//      user.setPassward("5438438");
//      user.setRealname("刘广");
//      return user;
//  }
    @Data
    public static class User implements Serializable {
        private String username;
        private String passward;
        private String realname;
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "Asia/Shanghai")
        private Date creatTime;
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "Asia/Shanghai")
        private Date modifyTime;
    }

    @ResponseBody
    @RequestMapping("/user")
    public Object User() {
        User user = new User();
        user.setUsername("小丑");
        user.setPassward("5438438");
        user.setRealname("刘广");
        Uservo uservo = new Uservo();
        uservo.setCreatTime(new Date());
        uservo.setUsername("梁耀云");
        uservo.setModifyTime(new Date());
        uservo.setRealname("123");
        uservo.setRealname("lyy");
        Result result = new Result();
        if (uservo != null) {
            return Result.success(uservo);
        } else {
            return Result.fail(null);
        }
    }

    @ResponseBody
    @RequestMapping("/exe01")
    private Result testresult(Integer age, String name, Date birth, Boolean gender, String[] hobby) {
        Map<String, Object> map = new HashMap<>(5);
        map.put("age", age);
        map.put("name", name);
        map.put("birth", birth);
        map.put("gender", gender);
        map.put("hobby", hobby);
        return Result.success(map);
    }

    @ResponseBody
    @RequestMapping("/exe02")
    private Result testUserParam(UserLoginParam userLoginParam) {
        return Result.success(userLoginParam);
    }
    @ResponseBody
    @RequestMapping("/exe03")
    private Result testRequestParam(@RequestParam
                                            (value = "real-name",
                                                    required = false,
                                                    defaultValue = "默认昵称")
                                                String realname, @RequestParam Integer id) {
        Map<String,Object>map=new HashMap<>(2);
        map.put("id",id);
        map.put("real-name",realname);
        return Result.success(map);
    }
}
